package comp418.blingo;




import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
/**
 * 
 * @author Jingbang
 *
 */
public class login extends Activity {
	
	private Button bt_LogIn = null;
	private Button bt_SignUp = null;
 
	
	private EditText username = null;
	private EditText password = null;
	//private EditText ip = null;
	private String user = null;
	private String pw = null;
	private Utility utility=null;
	
	private String ipaddress=null;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		utility=Utility.getUtility();
		username = (EditText) findViewById(R.id.username);
		password = (EditText) findViewById(R.id.password);
		//ip = (EditText) findViewById(R.id.ip);
		bt_LogIn = (Button) findViewById(R.id.login_bn);
		bt_SignUp = (Button) findViewById(R.id.signup_bn);
		bt_LogIn.setOnClickListener(new bt_LogInListener());
		bt_SignUp.setOnClickListener(new bt_SignUpListener());

		
		
		
	}

	class bt_LogInListener implements android.view.View.OnClickListener {
		public void onClick(View v) {
			// TODO Auto-generated method stub
			//ipaddress=ip.getText().toString();
			
			user = username.getText().toString();
			
			pw = password.getText().toString();
	
			
			
			
			if(user!=null && pw!=null){
				if(user=="")
					utility.showToast(login.this, "Please Input the Username");
				else if(pw=="")
					utility.showToast(login.this, "Please Input the Password");
				else {
					boolean str=false;
					//TODO:do login
					
					str=utility.login(user, pw);
					//String str=cs.login(user, pw,0);
					
						
						if(str!=false){
							utility.setUsername(user);
							Intent intent=new Intent();
							intent.putExtra("username", user);
							intent.setClass(login.this, language.class);
							
							login.this.startActivity(intent);
						}else{
							utility.showToast(login.this, "wrong username/password");
						}
					
				}
			}
		}
		
	}
	
	class bt_SignUpListener implements android.view.View.OnClickListener {

		public void onClick(View v)  {
			// TODO Auto-generated method stub
			
			//utility=Utility.getUtility();
			
			user = username.getText().toString();
			
			pw = password.getText().toString();
			
			
			if(user!=null && pw!=null){
				if(user=="")
					utility.showToast(login.this, "Please Input the Username");
				else if(pw=="")
					utility.showToast(login.this, "Please Input the Password");
				else {
					boolean str=false;
					//TODO:do signup
					
					str=utility.signUp(user, pw);
					//String str=cs.login(user, pw,0);
					
						
						if(str!=false){
							utility.setUsername(user);
							Intent intent=new Intent();
							intent.putExtra("username", user);
							intent.setClass(login.this, language.class);
							
							login.this.startActivity(intent);
						}else{
							utility.showToast(login.this, "already signup!");
						}
					
				}
			}
		}
	}

}
